package com.zj.study;

public class 最长回文子串_最大公共子串 {

    public static void main(String[] args) {
        System.out.println(longestPalindrome("bcd123dcb"));
    }


    public static String longestPalindrome(String s) {
        int[][] arr = new int[s.length()][s.length()];
        String reverse = new StringBuilder(s).reverse().toString();

        int maxLen = 0;
        int start = 0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < s.length(); j++) {
                if (s.charAt(i) == reverse.charAt(j)) {
                    if (i == 0 || j == 0) {
                        arr[i][j] = 1;
                    } else {
                        arr[i][j] = arr[i - 1][j - 1] + 1;

                    }
                }
                if (arr[i][j] > maxLen) {
                    int bef = s.length() - 1 - j;
                    if (i == bef + arr[i][j] - 1) {
                        maxLen = arr[i][j];
                        start = i - maxLen + 1;
                    }

                }
            }
        }
        return maxLen >= 1 ? s.substring(start, start + maxLen) : "-1";
    }


}
